package com.gcsoftware.web.service;

import com.gcsoftware.utils.GenerateIDs;
import com.gcsoftware.utils.MapUtils;
import com.gcsoftware.web.beans.T_GW_INTERFS;
import com.gcsoftware.web.beans.T_GW_INTERF_PARAMS;
import com.gcsoftware.web.beans.request.GetInterfReq;
import com.gcsoftware.web.beans.response.T_GW_INTERFS_RSP;
import com.gcsoftware.web.mapper.T_GW_INTERFMAPPER;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qs on 2018/3/29.
 */
@Service
public class T_GW_INTERF_SERVICE {
    @Autowired
    private T_GW_INTERFMAPPER mapper ;

    public List<T_GW_INTERFS> queryForList(String c_APP_ID, String c_INTERF_STATUS, String c_INTERF_NAME, String c_COMPANY_ID) {
        return mapper.queryInterfsForList(c_APP_ID,c_INTERF_STATUS,c_INTERF_NAME,c_COMPANY_ID);
    }

    public List<T_GW_INTERF_PARAMS> queryInterfParams(String c_INTERF_ID) {
        return mapper.queryIparamsList(c_INTERF_ID);
    }

    public MapUtils saveInterfs(GetInterfReq req , String save_type) {
        if(StringUtils.isBlank(req.getC_INTERF_ID())) {
            if (1 == insertInterf(req)) {
                if (CollectionUtils.isNotEmpty(req.getIparams())) {
                    insertIparams(req.getC_INTERF_ID(),req.getIparams());
                }
                return MapUtils.ok(save_type.equals("insert") ? "成功新增接口" : "修改接口成功").put("refresh",1);
            } else {
                return MapUtils.error(save_type.equals("insert") ? "新增接口失败" : "修改接口失败");
            }
        } else {
            if (1 == updateInterf(req)) {
                if (CollectionUtils.isNotEmpty(req.getIparams())) {

                    List<T_GW_INTERF_PARAMS> newParms = new ArrayList<>();
                    List<T_GW_INTERF_PARAMS> updParms = new ArrayList<>();

                    for (T_GW_INTERF_PARAMS prm : req.getIparams()) {
                        if (StringUtils.isNotBlank(prm.getC_IPARAMS_ID())) {
                            updParms.add(prm);
                        } else {
                            newParms.add(prm);
                        }
                    }

                    List<T_GW_INTERF_PARAMS> allParams = mapper.queryIparamsList(req.getC_INTERF_ID());
                    List<T_GW_INTERF_PARAMS> deleteParams = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(allParams)) {
                        if (CollectionUtils.isNotEmpty(updParms)) {
                            boolean toRemove = false;
                            for (T_GW_INTERF_PARAMS apr : allParams) {
                                toRemove = true;
                                for (T_GW_INTERF_PARAMS ipr : updParms) {
                                    if (apr.getC_IPARAMS_ID().equals(ipr.getC_IPARAMS_ID())) {
                                        toRemove = false;
                                        break;
                                    }
                                }
                                if (toRemove) deleteParams.add(apr);
                            }
                            if (CollectionUtils.isNotEmpty(deleteParams)) {
                                mapper.batchDeleteIparams(deleteParams);
                            }
                            updateIparams(updParms);

                        }
                    }
                    if (CollectionUtils.isNotEmpty(newParms )) {
                        insertIparams(req.getC_INTERF_ID(), newParms);
                    }

                }
                return MapUtils.ok("修改接口成功");
            }
            return MapUtils.error("修改接口失败");
        }
    }

    public int getMaxIntefId(){
        String id = mapper.getMaxId();
        int maxId = 0;
        if(StringUtils.isNotBlank(id)) {
            maxId = Integer.valueOf(id.replace(GenerateIDs.ID_TYPE_INTERF_ID,""));
        }
        return maxId;
    }

    public int getMaxIparamId(){
        String id = mapper.getMaxIparamId();
        int maxId = 0;
        if(StringUtils.isNotBlank(id)) {
            maxId = Integer.valueOf(id.replace(GenerateIDs.ID_TYPE_IPARAM_ID,""));
        }
        return maxId;
    }
    public int insertInterf(GetInterfReq req) {
        if (0 == GenerateIDs.getMaxInterfId()) {
            GenerateIDs.setMaxInterfId(getMaxIntefId());
        }
        req.setC_INTERF_ID(GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_INTERF_ID));
        if (null != mapper.get(req.getC_INTERF_ID(),null)) {
            GenerateIDs.setMaxInterfId(0);
            insertInterf(req);
        }
        return mapper.insertInterf(req);
    }

    public int insertIparams(String c_INTERF_ID,List<T_GW_INTERF_PARAMS> iparams) {
        for (T_GW_INTERF_PARAMS prm : iparams) {
            if (0 == GenerateIDs.getMaxIparamId()) {
                GenerateIDs.setMaxIparamId(getMaxIparamId());
            }
            prm.setC_IPARAMS_ID(GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_IPARAM_ID));
            if (null != mapper.getIParamsById(prm.getC_IPARAMS_ID())) {
                GenerateIDs.setMaxInterfId(0);
                insertIparams(c_INTERF_ID,iparams);
            }
            prm.setC_INTERF_ID(c_INTERF_ID);
            mapper.insertIParams(prm);
        }
        return 1;
    }

    public int updateInterf(GetInterfReq req) {
        return mapper.updateInterf(req);
    }

    public int updateIparams(List<T_GW_INTERF_PARAMS> iparams) {
        try {
            for (T_GW_INTERF_PARAMS prm : iparams) {
                mapper.updateIParams(prm);
            }
            return  1;
        } catch (Exception e) {
            return 0;
        }
    }

    public int deleteInterf(String c_INTERF_ID) {
        mapper.deleteInterf(c_INTERF_ID);
        mapper.deleteIparams(c_INTERF_ID);
        return 1;
    }

    public T_GW_INTERFS get(String c_INTERF_ID) {
        return mapper.get(c_INTERF_ID,null);
    }

    public T_GW_INTERFS_RSP getForRsp(String c_INTERF_ID) {
        return mapper.getForRsp(c_INTERF_ID,null);
    }

    public int updateInterfStatus(String c_INTERF_ID,String c_INTERF_STATUS){
        return mapper.updateInterfStatus(c_INTERF_ID,c_INTERF_STATUS);
    }


}
